/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditGroupView.java
 * Date: 10/03/2011
 */

package com.ingenius.sau.client.view;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.DualListField;
import com.extjs.gxt.ui.client.widget.form.DualListField.Mode;
import com.extjs.gxt.ui.client.widget.form.ListField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.ingenius.sau.client.model.UserBaseModel;
import com.ingenius.sau.client.presenter.EditGroupPresenter.EditGroupDisplay;

/**
 * Class <strong>EditGroupView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditGroupView extends AbstractEditView implements EditGroupDisplay {

	// UI Widgets
	private final TextField<String> textName;
	private final TextArea textDescription;
	private final DualListField<UserBaseModel> lists;
	private final ListStore<UserBaseModel> storeFrom;
	private final ListStore<UserBaseModel> storeTo;

	// Constructor

	public EditGroupView() {
		super();
		textName = new TextField<String>();
		textDescription = new TextArea();
		lists = new DualListField<UserBaseModel>();
		storeFrom = new ListStore<UserBaseModel>();
		storeTo = new ListStore<UserBaseModel>();

		// TODO i18n
		setHeading("Edit Group");
		setSize(500, 380);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.view.AbstractEditView#createComponents()
	 */
	@Override
	protected void createComponents() {
		// TODO i18n
		FormData formData = new FormData("-20");
		textName.setFieldLabel("Name");
		textName.setAllowBlank(false);
		textName.addPlugin(plugin);
		textName.setData("text", "Enter the group name");
		textName.getMessages().setBlankText("Name is required");

		textDescription.setFieldLabel("Description");
		textDescription.setHeight(75);
		textDescription.setAllowBlank(false);
		textDescription.addPlugin(plugin);
		textDescription.setData("text", "Any description is fine...");
		textDescription.getMessages().setBlankText("Description is required");

		lists.setMode(Mode.INSERT);
		lists.setFieldLabel("Users");
		lists.addPlugin(plugin);
		lists.setData("text", "Select the group's users (Drag & Drop)");

		ListField<UserBaseModel> from = lists.getFromList();
		from.setDisplayField("display");
		from.setStore(storeFrom);

		ListField<UserBaseModel> to = lists.getToList();
		to.setDisplayField("display");
		to.setStore(storeTo);

		form.add(textName, formData);
		form.add(textDescription, formData);
		form.add(lists, formData);
	}

	@Override
	public String getName() {
		return textName.getValue();
	}

	@Override
	public void setName(String name) {
		textName.setValue(name);
	}

	@Override
	public String getDescription() {
		return textDescription.getValue();
	}

	@Override
	public void setDescription(String description) {
		textDescription.setValue(description);
	}

	@Override
	public void addStoreFrom(List<UserBaseModel> users) {
		storeFrom.add(users);
	}

	@Override
	public void addStoreTo(List<UserBaseModel> users) {
		storeTo.add(users);
	}

	@Override
	public List<String> getUserIds() {
		List<String> result = new ArrayList<String>();
		for (int index = 0; index < storeTo.getCount(); index++) {
			result.add(storeTo.getAt(index).getId());
		}
		return result;
	}
}
